package com.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.salary.dao.EmployeesMapper;
import com.salary.dao.SalariesMapper;
import com.salary.entity.Employees;
import com.salary.entity.Jixiao;
import com.salary.entity.Salaries;
import com.salary.entity.view.SalariesView;
import com.salary.qpage.QPage;
import com.salary.service.IEmployeesService;
import com.salary.service.IJixiaoService;
import com.salary.service.ISalariesService;
import com.salary.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 员工工资业务接口实现层
 */
@Service
public class SalariesServiceImpl extends ServiceImpl<SalariesMapper, Salaries> implements ISalariesService {

    @Autowired
    private IJixiaoService jixiaoService;

    /**
     * 列表分页
     * @param qPage
     * @param obj
     * @return
     */
    @Override
    public IPage getList(QPage qPage, SalariesView obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        List<SalariesView> mapList = baseMapper.list(iPage,obj);
        return iPage.setRecords(mapList);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(Salaries obj) {
        //计算工资
        //获取当前员工绩效情况
        QueryWrapper<Jixiao> queryWrapper=new QueryWrapper<Jixiao>();
        queryWrapper.eq("employee_id",obj.getEmployeeId());
        List<Jixiao> jixiaos=this.jixiaoService.list(queryWrapper);
        if(jixiaos==null||jixiaos.size()==0){
            return Result.reError("请先填写改员工的绩效评价，再来计算工资");
        }
        Jixiao jixiao=jixiaos.get(0);
        BigDecimal jiangjin=BigDecimal.ZERO;
        jiangjin=obj.getJiangjin().multiply(jixiao.getZzpf());
        obj.setJiangjin(jiangjin);
        obj.setSfhj(jiangjin.add(obj.getMoney()));
        obj.setCtime(new Date());
        this.baseMapper.insert(obj);
        return Result.reOk("新增成功");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(Salaries obj) {
        //计算工资
        //获取当前员工绩效情况
        QueryWrapper<Jixiao> queryWrapper=new QueryWrapper<Jixiao>();
        queryWrapper.eq("employee_id",obj.getEmployeeId());
        List<Jixiao> jixiaos=this.jixiaoService.list(queryWrapper);
        if(jixiaos==null||jixiaos.size()==0){
            return Result.reError("请先填写改员工的绩效评价，再来计算工资");
        }
        Jixiao jixiao=jixiaos.get(0);
        BigDecimal jiangjin=BigDecimal.ZERO;
        jiangjin=obj.getJiangjin().multiply(jixiao.getZzpf());
        obj.setJiangjin(jiangjin);
        obj.setSfhj(jiangjin.add(obj.getMoney()));
        int isOk=this.baseMapper.updateById(obj);
        if(isOk==1){
            return Result.reOk("修改成功");
        }
        return Result.reError("修改失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteId(Integer id) {
        int isOk=this.baseMapper.deleteById(id);
        if(isOk==1){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除失败");
    }



}
